# import logging
#
#
# # logging.basicConfig(level=logging.DEBUG)
# # 使用basicConfig调整日志输出至文件，filename='demo01.log'
# # logging.basicConfig(filename='demo01.log', level=logging.DEBUG)
#
# # 清空后再写入filemode="w"
# # logging.basicConfig(filename='demo01.log', filemode="w", level=logging.DEBUG)
# logging.basicConfig(format="%(asctime)s|%(levelname)s|%(filename)s|%(lineno)s|%(message)s", level=logging.DEBUG)
#
# logging.debug("This is debug log")
# logging.info("This is info log")
# logging.warning("This is warning log")
# logging.error("This is error log")
# logging.critical("This is critical log")

# logging高级应用方式
import logging

# 创建一个记录器
logger = logging.getLogger()

# 设置记录器的日志级别,设置为:Debug级别
# logger.setLevel(logging.DEBUG)

# 创建文件处理器,输出日志到文件内
fh = logging.FileHandler(filename='my_log.log', encoding='utf-8')
fh.setLevel(logging.WARNING)
# 创建控制台处理器,输入日志到控制台
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)

# 定义输出格式
famater = logging.Formatter("%(asctime)s|%(levelname)s|%(filename)s|%(lineno)s|%(message)s")

# 给处理器设置输出格式
fh.setFormatter(famater)
ch.setFormatter(famater)

logger.addHandler(fh)
logger.addHandler(ch)

# logger.debug("这是debug日志")
# logger.info("这是info日志")
# logger.warning("这是warning日志")
# logger.error("这是error日志")
# logger.critical("这是critical日志")

try:
    1/0
except Exception as e:
    logger.error(e)
    logger.exception(e)

print("运行成功")